<template>
  <div class="app-container">
    <el-form :inline="true" :model="queryParams" ref="queryRef">
      <el-form-item label="时间区间">
        <el-date-picker
          v-model="queryParams.timeRange"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          value-format="yyyy-MM-dd HH:mm:ss"
        />
      </el-form-item>
      <el-form-item label="类型">
        <el-select v-model="queryParams.type" clearable @change="$emit('search', normalize(queryParams))">
          <el-option label="全部" :value="undefined" />
          <el-option
            v-for="item in equipmentTypeOptions"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="queryParams.status" clearable @change="$emit('search', normalize(queryParams))">
          <el-option label="全部" :value="undefined" />
          <el-option
            v-for="item in statusOptions"
            :key="item.code"
            :label="item.name"
            :value="item.code"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="名称">
        <el-input v-model="queryParams.assetName" placeholder="请输入名称" clearable @keyup.enter.native="$emit('search', normalize(queryParams))" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="$emit('search', normalize(queryParams))">搜索</el-button>
        <el-button @click="reset">重置</el-button>
        <el-button type="text" @click="folded = !folded">{{ folded ? '展开' : '收起' }}</el-button>
      </el-form-item>
      <div v-show="!folded">
        <el-form-item label="资产编号">
          <el-input v-model="queryParams.assetNo" placeholder="请输入资产编号" clearable @keyup.enter.native="$emit('search', normalize(queryParams))"/>
        </el-form-item>
        <el-form-item label="使用部门">
          <el-input v-model="queryParams.deptId" placeholder="全部" clearable @keyup.enter.native="$emit('search', normalize(queryParams))"/>
        </el-form-item>
        <el-form-item label="报修类型">
          <el-input v-model="queryParams.reportType" placeholder="全部" clearable @keyup.enter.native="$emit('search', normalize(queryParams))"/>
        </el-form-item>
        <el-form-item label="故障类型">
          <el-input v-model="queryParams.faultType" placeholder="全部" clearable @keyup.enter.native="$emit('search', normalize(queryParams))"/>
        </el-form-item>
        <el-form-item label="紧急程度">
          <el-input v-model="queryParams.urgency" placeholder="全部" clearable @keyup.enter.native="$emit('search', normalize(queryParams))"/>
        </el-form-item>
      </div>
    </el-form>
  </div>

</template>

<script>
export default {
  name: 'OrderSearchForm',
  props: {
    value: { type: Object, default: () => ({}) }
  },
  data() {
    return {
      folded: true,
      queryParams: {
        timeRange: undefined,
        type: undefined,
        status: undefined,
        assetName: undefined,
        assetNo: undefined,
        deptId: undefined,
        shiftName: undefined,
        reportType: undefined,
        faultType: undefined,
        urgency: undefined,
        description: undefined
      },
      equipmentTypeOptions: [],
      statusOptions: []
    }
  },
  created() {
    this.loadEquipmentTypeDict()
    this.loadStatusOptions()
  },
  methods: {
    // 加载设备类型字典数据
    async loadEquipmentTypeDict() {
      try {
        const request = (await import('@/utils/request')).default
        const res = await request({ url: '/system/type/list', method: 'get', params: { pageNum: 1, pageSize: 9999, enabled: 1 } })
        this.equipmentTypeOptions = (res.rows || []).map(item => ({
          dictValue: item.code,
          dictLabel: item.name
        }))
      } catch(e) { /* ignore */ }
    },
    // 加载状态选项（报修单状态）
    async loadStatusOptions() {
      try {
        const request = (await import('@/utils/request')).default
        const res = await request({ url: '/system/status/rpt-order/list', method: 'get', params: { pageNum: 1, pageSize: 9999, enabled: 1 } })
        this.statusOptions = res.rows || []
      } catch(e) {
        // 兜底数据
        this.statusOptions = [
          { code: 'DRAFT', name: '草稿' },
          { code: 'PENDING_ASSIGN', name: '待分配' },
          { code: 'PENDING_ACKNOWLEDGE', name: '待承认' },
          { code: 'ACKNOWLEDGED', name: '已承认' },
          { code: 'REJECTED', name: '已拒绝' },
          { code: 'IN_REPAIR', name: '维修中' },
          { code: 'CONFIRMED', name: '已确认' },
          { code: 'CLOSED', name: '已关闭' },
          { code: 'EXTRA_PROCESSING', name: '追加工中' }
        ]
      }
    },
    reset() {
      // 重置所有查询条件到初始状态
      this.queryParams = {
        timeRange: undefined,
        type: undefined,
        status: undefined,
        assetName: undefined,
        assetNo: undefined,
        deptId: undefined,
        reportType: undefined,
        faultType: undefined,
        urgency: undefined
      }
      this.folded = true
      this.$emit('reset')
    },
    normalize(q) {
      const p = { ...q }
      if (Array.isArray(q.timeRange) && q.timeRange.length === 2) {
        // 确保时间范围的准确性，处理时区问题
        const beginTime = q.timeRange[0] ? new Date(q.timeRange[0]).toISOString().slice(0, 19).replace('T', ' ') : null
        const endTime = q.timeRange[1] ? new Date(q.timeRange[1]).toISOString().slice(0, 19).replace('T', ' ') : null
        p.beginTime = beginTime
        p.endTime = endTime
      }
      delete p.timeRange
      return p
    }
  }
}
</script>


